package com.controller;

import com.pojo.Books;
import com.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
	//controller 调service层
	@Autowired
	@Qualifier("bookServiceImpl")
	private BookService bookService;

	//查询全部书籍，并且返回到一个书籍显示页面
	@RequestMapping("/allBook")
	public String list(Model model){
		List<Books> list = bookService.queryAllBook();
		model.addAttribute("list",list);
		return "allBook";
	}

	//跳转到增加书籍页面
	@RequestMapping("toAddBook")
	public String toAddPaper(){
		return "addBook";
	}

	//添加书籍请求
	@RequestMapping("/addBook")
	public String addBook(Books books){
		System.out.println("addBook"+books);
		bookService.addBook(books);
		return "redirect:/book/allBook"; //重定向到我们的@RequestMapping("/allBook")
	}

	//跳转到修改页面
	@RequestMapping("/toUpdate/{bookId}")
	public String toUpdatePaper(@PathVariable int bookId,Model model){
		Books books = bookService.queryBookById(bookId);
		model.addAttribute("Qbook",books);
		return "updateBook";
	}

	//修改书籍
	@RequestMapping("/updateBook")
	public String updateBook(Books books){
		System.out.println("uodateBook=>"+books);
		bookService.updateBook(books);
		return "redirect:/book/allBook";
	}

	//删除书籍
	@RequestMapping("/deleteBook/{bookId}")
	public String deleteBook(@PathVariable("bookId") int id){
		bookService.deleteBookById(id);
		return "redirect:/book/allBook";
	}

	//查询书籍
	@RequestMapping("/queryBook")
	public String queryBook(String queryBookName,Model model){
		Books books = bookService.queryBookByName(queryBookName);
		List<Books> list = new ArrayList<>();
		list.add(books);


		model.addAttribute("list",list);

		return "allBook";
	}
}
